<?php
/**
 *author:韩帅
 *createtime:2017/6/22 17:33
 *description:
 */
namespace App\Http\Controllers\App;

use App\Models\SystemBalance;
use App\Models\SystemLog;
use Illuminate\Http\Request;
use App\Models\UserShopBalance;
use Illuminate\Support\Facades\DB;

class SystemBalanceController extends BaseController
{
    /**
     * 队列中多余的钱分给平台
     */
    public function SystemIncome(Request $request) {

        $order_sn = $request->input('order_sn');
        $money    = number_format($request->input('money'),2);

        if (empty($order_sn) || empty($money)) {

            return self::error('字段不完整');
        }

        $objA = SystemBalance::find(1);
        $objB = new SystemLog();

        //开事务
        $res = false;
        DB::beginTransaction();
        try{

            $objA->balance  = $objA->balance+$money;
            $objA->save();

            $objB->order_sn = $order_sn;
            $objB->money    = $money;
            $objB->type     = SystemLog::TYPE;
            $objB->save();

            // 提交事务
            DB::commit();
            $res = true;
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
        }

        if ($res == true) {
            return self::success('成功');
        }
        return self::json($e,'失败',400);
    }
}